'use strict';
module.exports = {
	/**
	 * 添加订单
	 * @url client/resource/kh/orderAdd 前端调用的url参数地址
	 * data 请求参数 说明
	 * @param {String} _id	ID
	 * @param {String} resource_cover  封面
	 * @param {String} dbank_url	网盘链接
	 * @param {String} dbank_code 	网盘提取码  
	 * @param {Number} issue_type 发布形式  1积分资源  2VIP专享资源  3付费资源
	 * @param {String} pay_money	支付金额/积分
	 * @param {String} order_no		订单号
	 * @param {Number} status 状态  1待激活  2已激活
	 */
	main: async (event) => {
		let { data = {}, userInfo, util, filterResponse, originalParam } = event;
		let { customUtil, uniID, config, pubFun, vk, db, _ } = util;
		let { uid } = data;
		let res = { code: 0, msg: "" };
		// 业务逻辑开始-----------------------------------------------------------
		let { _id, pay_money } = data;
		
		let dbName = "db-order";
		
		let dbRes = await vk.baseDao.findById({
			dbName: "db-resource",
			id: _id
		})
		
		if(!vk.pubfn.isNull(dbRes)) {
			let money = dbRes.issue_type === 1? dbRes.integral_num: dbRes.price;
			let dataJson = data;
			dataJson["resource_id"] = _id;
			dataJson["title"] = dbRes.title;
			dataJson["resource_cover"] = dbRes.resource_cover;
			dataJson["dbank_url"] = dbRes.dbank_url;
			dataJson["dbank_code"] = dbRes.dbank_code;
			dataJson["order_no"] = vk.pubfn.createOrderNo("NO", 25);
			dataJson["title"] = dbRes.title;
			dataJson["status"] = dbRes.issue_type === 1? 2: 1;
			dataJson["issue_type"] = dbRes.issue_type;
			delete dataJson["_id"];
			delete dataJson["need_user_info"];
			if(dbRes.issue_type !== 2) {
				if(pay_money == money) {
					let integralData;
					if(dbRes.uid != userInfo._id) {
						if(dbRes.issue_type === 1 && userInfo.integral < pay_money) {
							return {code: -1, msg: "支付失败，积分不足！"}
						}
						if(dbRes.issue_type === 1) {
							integralData = await vk.baseDao.updateById({
								dbName: 'uni-id-users',
								id: userInfo._id,
								dataJson: {
									integral: _.inc(parseFloat('-' + pay_money))
								}
							})
							if(!integralData) {
								return {code: -1, msg: "支付失败，请联系客服！"}
							}
						}
						let dbData = await vk.baseDao.add({
							dbName,
							dataJson
						})
						if(!vk.pubfn.isNull(dbData)) {
							if(dbRes.issue_type !== 3) {
								res["data"] = {
									dbank_url: dataJson.dbank_url,
									dbank_code: dataJson.dbank_code
								}
							} else {
								res["data"] = {
									order_no: dataJson.order_no
								}
							}
							if(dbRes.issue_type === 1) {
								integralData = await vk.baseDao.updateById({
									dbName: 'uni-id-users',
									id: dbRes.uid,
									dataJson: {
										integral: _.inc(parseFloat(pay_money))
									}
								})
							}
						} else {
							if(dbRes.issue_type === 1) {
								integralData = await vk.baseDao.updateById({
									dbName: 'uni-id-users',
									id: userInfo._id,
									dataJson: {
										integral: _.inc(parseFloat(pay_money))
									}
								})
							}
						}
					} else {
						res["data"] = {
							dbank_url: dataJson.dbank_url,
							dbank_code: dataJson.dbank_code
						}
					}
				} else {
					return {code: -1, msg: "支付有误，请联系客服！"}
				}
			} else {
				if(userInfo.vip > 0) {
					let dbData = await vk.baseDao.add({
						dbName,
						dataJson
					})
					res["data"] = {
						dbank_url: dataJson.dbank_url,
						dbank_code: dataJson.dbank_code
					}
				} else {
					return {code: -1, msg: "VIP资源，请联系客服开通VIP！"}
				}
			}
		} else {
			return {code: 404, msg: "创建订单失败或已下架！"}
		}
		// 业务逻辑结束-----------------------------------------------------------
		return res;
	}
}
